package com.example.demo.controller;

import com.example.demo.commen.AppResult;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.User;
import com.example.demo.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(tags = "用户接口")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    @ApiOperation("注册")
    @RequestMapping(method = RequestMethod.POST,value = "/signUp")
    public AppResult signUp(@ApiParam("用户名") @RequestParam(value = "username",required = true)String username,
                           @ApiParam("密码") @RequestParam(value = "password",required = true) String password){

        if(username==null||!StringUtils.hasLength(username)||password==null||!StringUtils.hasLength(password)){
            throw new ApplicationException("参数校验异常");
        }
        userService.insertUser(username,password);
        return AppResult.success("注册成功");

    }


    @ApiOperation("登录")
    @RequestMapping(method = RequestMethod.POST,value = "/login")
    public  AppResult login (HttpServletRequest request,
                            @ApiParam("用户名") @RequestParam(value = "username",required = true)String username,
                             @ApiParam("密码")@RequestParam(value = "password",required = true) String password){
        if(username==null||!StringUtils.hasLength(username)||password==null||!StringUtils.hasLength(password)){

            throw new ApplicationException("参数校验异常");
        }

      boolean flag =  userService.login(username,password);

        if(flag!=true){
            throw new ApplicationException("登录失败");
        }
        User user = userService.selectUserByName(username);
        HttpSession session  = request.getSession(true);
        session.setAttribute("userInfo",user);
        return AppResult.success("登录成功");
    }

       @ApiOperation("游戏界面-玩家信息")
    @RequestMapping(method = RequestMethod.GET,value = "/userInformation")

    public AppResult<User> getUserInformation(HttpServletRequest request){

         System.out.println("进入");
        HttpSession session = request.getSession(false);
       User user = (User) session.getAttribute("userInfo");
       log.info(user.toString());
       if(user==null||user.getUserId()<=0){
           throw new ApplicationException("用户不存在");
       }

      User existUser = userService.selectUserById(user.getUserId());    //这里是必要得的，因为如果这里不重新从数据库获取数据
                                                                      //那当我们游戏结束返回到游戏大厅时，这里拿的信息就会时原来
                                                                     //session存储用户的信息，所以就没更新，

          return AppResult.success(existUser);
       }


}
